//
//  HEPayPopView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEPayPopView.h"
#import "HEOrderPayCell.h"


#define panelWidth 300
#define cellHeight 55
@interface HEPayPopView () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *rows;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation HEPayPopView

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
    }
    return _formModel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self=[super initWithFrame:frame]) {
        
    }
    return self;
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, 1 , panelWidth, 60) style:UITableViewStylePlain];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        [_tableView registerClass:[HEOrderPayCell class] forCellReuseIdentifier: @"HEOrderPayCell"];
    }
    return _tableView;
}

- (void)initCell {
    _rows = [NSMutableArray array];
    
    for (HEPaymentModel * item in _paymentsList) {
        // 支付方式
        GDFiterCellModel *payModel = [GDFiterCellModel new];
        payModel.cellKlass = HEOrderPayCell.class;
        payModel.cellType = GDFiterCellTypeOrderPay;
        [_rows addObject:payModel];
    }
    
    self.formModel.order_payments = _paymentsList;
    [self.tableView reloadData];
}

- (void)setPaymentsList:(NSArray<HEPaymentModel *> *)paymentsList {
    _paymentsList = paymentsList;
    
    [self initUI];
    [self initCell];
}

- (void)initUI {
     
    
    CGFloat h = 50 + cellHeight * _paymentsList.count + 49 + 60;
    
    _contentView = [[UIView alloc] init];
    _contentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [self addSubview:_contentView];
    

    UIView *warp = [[UIView alloc] init];
    warp.backgroundColor = KWhiteColor;
    [_contentView addSubview:warp];
    
    UIView *headLine = [[UIView alloc] init];
    headLine.backgroundColor = CSplitLineColor;
    [warp addSubview:headLine];
    
    // 标题
    UILabel *titleLb = [[UILabel alloc] init];
    titleLb.text = @"订单支付";
    titleLb.font = DYFont(18);
    titleLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:titleLb];
     
    // 关闭
    UIImageView *closeView = [[UIImageView alloc] init];
    closeView.image = IMAGE_NAMED(@"btn_close");
    closeView.userInteractionEnabled = YES;
    
    [warp addSubview:closeView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeClicks)];
    tap.delegate = self;
    [closeView addGestureRecognizer:tap];
    
    // 金额
    UILabel *priceLb = [UILabel new];
    priceLb.text = [NSString stringWithFormat:@"￥%@", [NSDecimalNumber rounding:_detials.payAmount afterPoint:2]];
    priceLb.textColor = CPriceColor;
    priceLb.font = DYBlodFont(18);
    priceLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:priceLb];
    
    priceLb.qmui_borderPosition = QMUIViewBorderPositionBottom;
    priceLb.qmui_borderWidth = 1;
    priceLb.qmui_borderColor = CSplitLineColor;
    
    // 支付方式
    [warp addSubview: self.tableView];

    // 支付
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [payBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    payBtn.titleLabel.font = DYFont(16);
    [payBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [warp addSubview:payBtn];
    [payBtn addTarget:self action:@selector(toPay) forControlEvents:UIControlEventTouchUpInside];
    payBtn.qmui_borderPosition = QMUIViewBorderPositionTop;
    payBtn.qmui_borderWidth = 1;
    payBtn.qmui_borderColor = CSplitLineColor;
    
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];

    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(panelWidth);
        make.centerY.equalTo(_contentView);
        make.centerX.equalTo(_contentView);
        make.height.mas_offset(h);
    }];
    [warp hg_setAllCornerWithCornerRadius:10.0f];


    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp);
        make.left.right.equalTo(warp);
        make.height.mas_offset(45);
    }];

    [closeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLb);
        make.right.equalTo(warp).mas_offset(-15);
        make.width.height.mas_offset(23);
    }];



    [headLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLb.mas_bottom);
        make.left.right.equalTo(warp);
        make.height.mas_offset(1);
    }];

    [priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headLine.mas_bottom);
        make.left.right.equalTo(warp);
        make.height.mas_offset(60);
    }];

    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceLb.mas_bottom);
        make.left.right.equalTo(warp);
        make.height.mas_offset(cellHeight * _paymentsList.count);
    }];

    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView.mas_bottom);
        make.left.right.equalTo(warp);
        make.height.mas_offset(49);
        make.bottom.equalTo(warp.mas_bottom);
    }];
}


#pragma mark -
- (void)closeClicks {
    [self hideView];
}

- (void)hideView {
    [UIView animateWithDuration:0.3f animations:^{
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self restSelect];
        self.contentView.hidden = YES;
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}

- (void)toPay {
    
    if ([DYTools isBlankString:self.formModel.order_payType]) {
        [Dialog toastBottom:@"请选择支付方式~"];
        return;
    }
    
    if ([_delegate respondsToSelector:@selector(popView:payType:)]) {
        [_delegate popView:self payType:self.formModel.order_payType];
    }
    [self hideView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return self.rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GDFiterCellModel *fiterModel = _rows[indexPath.row];
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return cellHeight;
}

#pragma mark 解决cell分割线左侧留空的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 下面这几行代码是用来设置cell的上下行线的位置
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    //按照作者最后的意思还要加上下面这一段，才能做到底部线控制位置，所以这里按stackflow上的做法添加上吧。
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEPaymentModel *payModel = self.formModel.order_payments[indexPath.row];
    self.formModel.order_payType = payModel.code;
    [self changeSelectPayType:indexPath.row];
 }

- (void)changeSelectPayType:(NSInteger)index {
    
    for (int i = 0; i < self.formModel.order_payments.count; i++) {
        HEPaymentModel *payModel = self.formModel.order_payments[i];
        if (index == i) {
            payModel.isChoose = YES;
        }else {
            payModel.isChoose = NO;
        }
    }
    
    [_tableView reloadData];
}

- (void)restSelect {
    for (int i = 0; i < self.formModel.order_payments.count; i++) {
        HEPaymentModel *payModel = self.formModel.order_payments[i];
        payModel.isChoose = NO;
    }
    [_tableView reloadData];
}


#pragma mark -





@end
